#ifndef __LAUNCHER_H__
#define __LAUNCHER_H__

#include "cocos2d.h"
USING_NS_CC;
namespace suta
{
	class LauncherListener
	{
	public:
		virtual void onStartAiming()=0;
		virtual void onEndAiming(float dx,float dy)=0;
		virtual void onMoveAiming(float dx,float dy)=0;
	};

	class Launcher:public CCLayer
	{
		LauncherListener *m_pLauncherListener;
		float m_fStartX;
		float m_fStartY;
		CCSprite*m_pBgSprite;
		CCParticleFire *m_pFire;

		inline void fastRotate()
		{
			m_pBgSprite->stopAllActions();
			CCRotateBy*rotate=CCRotateBy::actionWithDuration(1,720);
			CCRepeatForever*forever=CCRepeatForever::actionWithAction(rotate);
			m_pBgSprite->runAction(forever);

			m_pFire->setIsVisible(true);
		};

		inline void slowRotate()
		{
			m_pBgSprite->stopAllActions();
			CCRotateBy*rotate=CCRotateBy::actionWithDuration(5,360);
			CCRepeatForever*forever=CCRepeatForever::actionWithAction(rotate);
			m_pBgSprite->runAction(forever);

			m_pFire->setIsVisible(false);
		};
	public:

		Launcher():
		  CCLayer(),
		  m_pLauncherListener(NULL),
		  m_fStartX(0),
		  m_fStartY(0),
		  m_pBgSprite(NULL),
		  m_pFire(NULL)
		  {};

		bool init(LauncherListener *launcherListener)
		{
			bool ret=false;
			do 
			{
				CC_BREAK_IF(!CCLayer::init());
				m_pLauncherListener=launcherListener;
				setContentSize(CCSizeMake(80,80));
				setIsTouchEnabled(true);
				setIsRelativeAnchorPoint(true);
				{
					//bg
					m_pBgSprite=CCSprite::spriteWithFile("launcher.png");
					m_pBgSprite->setPosition(ccp(40,40));
					addChild(m_pBgSprite);
				}

				{
					//fire
					m_pFire=CCParticleFire::node();
					m_pFire->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
					m_pFire->setPosition(m_pBgSprite->getPosition());
					addChild(m_pFire);
					m_pFire->setIsVisible(false);
				}

				slowRotate();
				ret=true;
			} while (0);
			return ret;
		};

		LAYER_NODE_FUNC_PARAM(Launcher,LauncherListener*,listener);
		
		virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
		{
			CCPoint p=CCDirector::sharedDirector()->convertToGL(pTouch->locationInView());
			bool ret= ((p.x>=getPositionX()-getContentSize().width/2)&&(p.x<=getPositionX()+getContentSize().width/2)&&(p.y>=getPositionY()-getContentSize().height/2)&&(p.y<=getPositionY()+getContentSize().height/2));
			if (ret&&m_pLauncherListener)
			{
				m_pLauncherListener->onStartAiming();
				m_fStartX=p.x;
				m_fStartY=p.y;
			}

			if (ret)
			{
				fastRotate();
			}
			
			return ret;
		};

		virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
		{
			CCPoint p=CCDirector::sharedDirector()->convertToGL(pTouch->locationInView());
			if (m_pLauncherListener)
			{
				m_pLauncherListener->onMoveAiming(p.x-m_fStartX,p.y-m_fStartY);
			}
		};

		virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
		{
			CCPoint p=CCDirector::sharedDirector()->convertToGL(pTouch->locationInView());
			if (m_pLauncherListener)
			{
				m_pLauncherListener->onEndAiming(p.x-m_fStartX,p.y-m_fStartY);
			}

			slowRotate();
		};

		virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
		{
			slowRotate();
		};

		virtual void registerWithTouchDispatcher()
		{
			CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true);
		};
	};
}

#endif//__LAUNCHER_H__